package com.lance.answer.gui.answer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p><p>
 *
 * @author : fengx
 * @since JDK8.0
 * create by fengx on 2021/7/15 9:34
 */
public class DealJsonString2 {

    public static void main(String[] args) throws Exception {




        String courseName = "497ec914-9d06-408d-8dd2-3e39219841b6";
        //D:\\answer\\一体化\\deal\\
        for(File file:new File("D:\\answer\\一体化").listFiles()){
            String fileName = file.getName();
            if("deal".equals(fileName)){
                continue;
            }
            if(!file.isDirectory()){

                continue;
            }
            for(File childFile:file.listFiles()){
                // String dest = "D:\\answer\\一体化\\"+"deal\\"+file.getName();
                String dest = "D:\\answer\\一体化\\"+"deal\\"+file.getName();
              try{
                  fix(new FileInputStream(childFile),dest);
              }catch (Exception e){
                  System.out.println(childFile.getName());
                  Thread.sleep(200000);
              }
            }

        }




    }

    public static void fix(InputStream inputStream,String dest) throws IOException {

        StringBuilder sb = new StringBuilder();
        for(String s:IOUtils.readLines(inputStream)){
            sb.append(s);
        }
        System.out.println(sb.toString());

        GkHomework homework =    JSON.parseObject(sb.toString(),GkHomework.class);
        System.out.println(homework);

        List<GkHomework.ContentDTO.PaperInfoDTO.ItemsDTO> items =  homework.getContent().getPaperInfo().getItems();

        List<Question> questions = Lists.newArrayList();
        String name = homework.getContent().getPaperInfo().getModel().getP2();

        for(GkHomework.ContentDTO.PaperInfoDTO.ItemsDTO item:items){
            // I1 itemId
            String itemId = item.getI1();
            // I2 题目
            String tile = item.getI2();
            // I6 答案集合 不是正确答案
            // FIXME 题目类型在Sections里面
            List<String> answers = item.getI6();
            Question question = new Question();
            questions.add(question);
            question.setQuestionCode(itemId);
            question.setTitle(tile);
            List<com.lance.answer.dto.Item> questionItems = Lists.newArrayList();
            //Choices

            System.err.println(" item.getChoices():"+ item.getChoices());

            List<String> correctAnswer = Lists.newArrayList();
            for(GkHomework.ContentDTO.PaperInfoDTO.ChoicesDTO choices:item.getChoices()){
                //  fixme 0是对  1是错  这个要处理一下  而且有的答案里面有标签 需要用正则表达式去掉
                com.lance.answer.dto.Item questionItem = new com.lance.answer.dto.Item(choices.getI2(),choices.getI1());
                questionItems.add(questionItem);
                if(choices.isCorrect()){
                    correctAnswer.add(choices.getI2());
                }
            }
            //大于1说明是多选
            if(correctAnswer.size()>1){
                question.setQuestionType(2);
                question.setQuestionTypeDesc("多选题");
            }
            if(correctAnswer.size()==1){
                question.setQuestionType(1);
                question.setQuestionTypeDesc("单选题");
            }
            // 如果是getChoices()为空说明是简单题
            if(correctAnswer.isEmpty()){
                correctAnswer.addAll(answers);
                question.setQuestionType(5);
                question.setQuestionTypeDesc("简答题");
            }

            String[] correctArray = new String[]{};
            question.setCorrectArray(correctAnswer.toArray(correctArray));
            question.setItems(questionItems);
        }


        List<Map<String,Object>> answers = Lists.newArrayList();
        for (Question question:questions) {
            String title = question.getTitle();
            title = Util.dealContent(title);
            Map<String,Object> answerMap = new HashMap<>();
            answerMap.put("questionType",question.getQuestionType());

            if(question.getQuestionType()==5){
                answerMap.put("answerType","send");
            }else {
                answerMap.put("answerType","click");
            }
            answerMap.put("questionTypeDesc",question.getQuestionTypeDesc());
            answerMap.put("questionTitle",title);
            //   answerMap.put("questionCode", MD5Utils.encode(subjectName + title));
            answerMap.put("questionCode",question.getQuestionCode());
            answerMap.put("items",question.getItems());
            answerMap.put("answers",question.getCorrectArray());
            answers.add(answerMap);
        }

    //    FileUtils.write(new File("D:\\answer\\一体化\\deal\\"+name+".json"), JSONObject.toJSONString(answers, SerializerFeature.PrettyFormat),"utf-8");
        FileUtils.write(new File(dest+name+".json"), JSONObject.toJSONString(answers, SerializerFeature.PrettyFormat),"utf-8");


    }


    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式

    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
    private static final String regEx_space = "\\s*|\t|\r|\n";// 定义空格回车换行符
    private static final String regEx_w = "<w[^>]*?>[\\s\\S]*?<\\/w[^>]*?>";//定义所有w标签


    /**
     * @param htmlStr
     * @return 删除Html标签
     * @author LongJin
     */
    public static String delHTMLTag(String htmlStr) {
        Pattern p_w = Pattern.compile(regEx_w, Pattern.CASE_INSENSITIVE);
        Matcher m_w = p_w.matcher(htmlStr);
        htmlStr = m_w.replaceAll(""); // 过滤script标签


        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签


        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签


        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签


        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签


        htmlStr = htmlStr.replaceAll("&nbsp;", ""); //过滤&nbsp;
        return htmlStr.trim(); // 返回文本字符串
    }
}
